<template>
  <div class="login">
    <div class="leftBg">
      <div class="title">欢迎登录</div>
      <div class="subTitle">完整的vue项目框架 - <strong>框架系统</strong></div>
    </div>
    <div class="rightLogin">
      <div class="loginContent">
        <a-form-model ref="ruleForm" :model="ruleForm" :rules="rules" v-bind="layout">
          <a-form-model-item has-feedback label="用户名" prop="username">
            <a-input v-model="ruleForm.username" />
          </a-form-model-item>
          <a-form-model-item has-feedback label="密码" prop="password">
            <a-input v-model="ruleForm.password" :type="showPassword ? 'text' : 'password'" autocomplete="off">
              <div slot="suffix" @click="changePasswordShow()">
                <img style="width: 20px; height: 20px; cursor: pointer" :src="getImg()" />
              </div>
            </a-input>
          </a-form-model-item>
          <a-form-model-item :wrapper-col="{ span: 14, offset: 6 }">
            <a-button type="primary" @click="submitForm('ruleForm')"> 登录 </a-button>
            <a-button style="margin-left: 10px" @click="resetForm('ruleForm')"> 重置 </a-button>
          </a-form-model-item>
        </a-form-model>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: 'admin',
        password: '111111',
      },
      rules: {
        password: [{ validator: validatePass, trigger: 'change' }],
      },
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 14 },
      },
      showPassword: false,
    }
  },
  watch: {
    showPassword() {
      this.getImg()
    },
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.handleSubmit()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.ruleForm = {
        username: '',
        password: '',
      }
    },
    handleSubmit() {
      this.$store
        .dispatch('user/login', this.ruleForm)
        .then(() => {
          this.$router.push({ path: '/' })
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    getImg() {
      if (this.showPassword) {
        return require('@/assets/images/login/eye_open.png')
      } else {
        return require('@/assets/images/login/eye_close.png')
      }
    },
    changePasswordShow() {
      this.showPassword = !this.showPassword
    },
  },
}
</script>

<style lang="scss" scoped>
.login {
  /*no*/
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  background-color: #f0f0f0;
  .leftBg {
    flex: 1;
    background-image: url('~@/assets/images/login/login_bg.jpg');
    background-size: 100% 100%;
    -moz-background-size: 100% 100%;
    background-repeat: no-repeat;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding-top: 100px;
    padding-left: 50px;
    .title {
      font-size: 46px;
      font-weight: bold;
    }
    .subTitle {
      font-size: 36px;
    }
  }
  .rightLogin {
    width: 35%;
    min-width: 450px; /*no*/
    max-width: 700px; /*no*/
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .loginContent {
      width: 80%;
      min-width: 400px; /*no*/
      background-color: #ffffff;
      box-shadow: 0px 2px 10px 0px rgba(31, 56, 88, 0.15); /*no*/
      border-radius: 5px; /*no*/
      height: 50%;
      min-height: 450px; /*no*/
      max-height: 700px; /*no*/
      overflow-y: auto;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      iframe {
        width: 100%;
        height: 100%;
        border: none;
      }
    }
  }
}
</style>
<style lang="scss">
.loginContent {
  .ant-form {
    font-size: 18px; /*no*/
    width: 90%;
    label {
      font-size: 18px; /*no*/
    }
  }
  .ant-form-item {
    font-size: 16px; /*no*/
    margin-bottom: 20px !important; /*no*/
  }
  .ant-form-item-label {
    line-height: 40px; /*no*/
  }
  .ant-form-item-control {
    line-height: 40px; /*no*/
  }
  .ant-input {
    height: 40px; /*no*/
    font-size: 16px; /*no*/
    padding: 0 10px; /*no*/
  }
  .ant-btn {
    width: 80px; /*no*/
    height: 40px; /*no*/
  }
}
</style>
